package com.example.plcopne.utils;

import android.util.Log;

import com.lezhu.plcsdklibrary.CmdUtil;
import com.lezhu.plcsdklibrary.Constants;
import com.lezhu.plcsdklibrary.NoDoubleClickUtils;
import com.lezhu.plcsdklibrary.callback.CmdListener;
import com.lezhu.plcsdklibrary.callback.DoorListener;
import com.lezhu.plcsdklibrary.callback.OpenDoorListener;
import com.lezhu.plcsdklibrary.manager.DoorStatusCallBackManager;
import com.lezhu.plcsdklibrary.manager.OpenDoorCallBackManager;

import top.keepempty.sph.library.SerialPortHelper;
import top.keepempty.sph.library.SphCmdEntity;
import top.keepempty.sph.library.SphResultCallback;

/**
 * @ClassName PlcUtils
 * @Description TODO
 * @Author zdl
 * @Date 2021/1/11 9:47
 * @Version 1.0
 */
public class PlcUtils {
    private static final String TAG = "PlcUtils";
    private static PlcUtils plcUtils;

    private static SerialPortHelper serialPortHelper;

    public static PlcUtils getInstance() {
        if (plcUtils == null) {
            synchronized (PlcUtils.class) {
                if (plcUtils == null) {
                    plcUtils = new PlcUtils();
                }
            }
        }
        return plcUtils;
    }

    public void init() {
        if (serialPortHelper == null) {
            serialPortHelper = new SerialPortHelper(16);
        }


        if (!serialPortHelper.isOpenDevice()) {
            boolean b = serialPortHelper.openDevice("/dev/ttyS4", 9600);
            if (!b) {
                if (cmdListener != null) {
                    cmdListener.cmd(2, "串口打开失败");
                }
            }
            serialPortHelper.setSphResultCallback(new SphResultCallback() {
                @Override
                public void onSendData(SphCmdEntity sendCom) {
                    Log.e("tag", "串口发送: "+sendCom.commandsHex);
                }

                @Override
                public void onReceiveData(SphCmdEntity data) {
                    Log.e("tag", "串口接收"+data.commandsHex);

                    if (cmdListener != null) {
                        cmdListener.cmd(2, data.commandsHex);
                    }
                    CmdUtil.processResultCmd(data.commandsHex);
                }

                @Override
                public void onComplete() {
                    Log.e("tag", "串口完成");
                }
            });
        }

    }


    public void OpenDoor(int posi) {
        String cmd = Constants.getCMD(posi);
        serialPortHelper.addCommands(cmd);
    }

    public void addDoorListener(DoorListener doorListener) {
        DoorStatusCallBackManager.getInstance().addDoorListener(doorListener);
    }


    public void requestOpenDoor(int pos, OpenDoorListener openDoorListener) {
        if (!NoDoubleClickUtils.isCanClick()) {
            Log.e(TAG, "请勿频繁调用");
            return;
        }
        init();
        OpenDoorCallBackManager.getInstance().addDoorListener(pos, openDoorListener);
        OpenDoor(pos);
    }

    private CmdListener cmdListener;

    public void addCmdListener(CmdListener cmdListener) {
        this.cmdListener = cmdListener;
    }

}
